package com.ubivelox.uienginesample_20100726_002;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;

public class MyLayout extends FrameLayout {
	//=============================================================================================
	private Canvas	mCanvas		= null;
	private Bitmap	mBitmap		= null;
	private Paint	mPaintClear	= null;
	private Paint	mPaintAlpha	= null;
	//-----------------------------------------------------------------------------------
	OnUpdateLayout	mOnUpdateLayout = null;
	public interface OnUpdateLayout {
		void UpdateLayout( Bitmap bitmap );
	}
	public void setOnUpdateLayout( OnUpdateLayout onUpdateLayout ) {
		mOnUpdateLayout = onUpdateLayout;
	}
	//=============================================================================================
	public MyLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mBitmap = Bitmap.createBitmap( 256, 256, Config.RGB_565);
		mCanvas = new Canvas( mBitmap );
		mPaintClear	= new Paint();
		mPaintAlpha	= new Paint();
//		mPaintAlpha.setAlpha( 80 );		// 0~255
	}
	//=============================================================================================
	@Override
	protected void dispatchDraw(Canvas canvas) {
		long time = SystemClock.uptimeMillis();
		if( mBitmap == null ||
			mBitmap.getWidth() != getWidth() ||
			mBitmap.getHeight() != getHeight() ) {
//			mBitmap = Bitmap.createBitmap( getWidth(), getHeight(), Config.RGB_565 );
			mCanvas.setBitmap(mBitmap);
		}
		else mCanvas.drawPaint(mPaintClear);
		super.dispatchDraw(mCanvas);
		//-------------------------------------------------------------------------------
		if( mOnUpdateLayout != null ) {
			mOnUpdateLayout.UpdateLayout( mBitmap );
		}
		//-------------------------------------------------------------------------------
		canvas.drawBitmap(mBitmap, 0, 0, mPaintAlpha);
		long time_1 = SystemClock.uptimeMillis();
//		Log.d("xxxxxxxxxxxxxxxxxxxx", String.valueOf(time_1 - time));
	}
	//=============================================================================================
}
